define(['store', 'mSearch', 'knockout'], function (Store, Mearch, ko) {
    function ViewModel(params) {
        var scope = this;
        var record = new Mearch();

        if(params){
            record.codes(params.codes);
            record.keyword(params.keyword);
        }

        scope.card = {
            title: '综合查询',
            css:{'xgx-search-card':true},
            cardBodyTop: {
                name: 'cmp:SearchForm',
                params: {
                    record: record,
                    onFormButtonClick:function(obj) {
                        if(obj.name == 'primary') {
                            if (obj.record.checkValidity()) {
                                var codes = obj.record.codes();
                                var keyword = obj.record.keyword();
                                var texts = obj.record._texts;
                                var tempHash = '#/Search&{0}'.formatTpl(JSON.js2Uri({codes:codes,keyword:keyword,texts:texts}));

                                if(location.hash != tempHash){
                                    scope.card.cardBody.params.components.removeAll();
                                    location.hash = '#/Search&{0}'.formatTpl(JSON.js2Uri({codes:codes,keyword:keyword,texts:texts}));
                                }


                                // if(codes.length == texts.length){
                                //     for(var cIndex = 0;cIndex<codes.length;++cIndex){
                                //         scope.card.cardBody.params.components.push({
                                //             name: 'cmp:ArchiveList', params: {code: codes[cIndex], codeName: texts[cIndex],keyword:keyword}
                                //         });
                                //     }
                                // }
                            }
                        }
                        else {
                            location.hash = '#/Search';
                        }
                    }
                }
            },
            cardBody: {
                name: 'cmp:Components',
                params: {
                    components: ko.observableArray()
                }
            }
        };

        if(params&&params.codes.length == params.texts.length){
            for(var cIndex = 0;cIndex<params.codes.length;++cIndex){
                scope.card.cardBody.params.components.push({
                    name: 'cmp:ArchiveList', params: {code: params.codes[cIndex], codeName: params.texts[cIndex],keyword:params.keyword}
                });
            }
        }
    }

    return ViewModel;
});
